let express = require('express');
let router = express.Router();

const harmService = require('../service/harmService');
const checkJWTMiddleWare = require('../middleWare/checkJWTMiddleWare');

// 此路由需要登录后才能访问
router.use(checkJWTMiddleWare);

/*
 * 查询所有危机类型接口
 */
router.get('/getAllHarmType', async function (req, res, next) {
    let result = await harmService.getAllHarmType();
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 新增危机档案接口
 */
router.post('/addHarmRecord', async function (req, res, next) {
    let studentId = req.body.studentId;
    let time = req.body.time;
    let address = req.body.address;
    let crisisTypeId = req.body.crisisTypeId;
    let note = req.body.note;
    if (!studentId || !time || !address || !crisisTypeId) {
        res.status(400).json({ success: false, msg: "参数不足，新增失败" });
        return ;
    }
    if (!note) note = "";
    let data = { studentId, time, address, crisisTypeId, note };
    let result = await harmService.addHarmRecord(req.jwtResult.data.uid, req.jwtResult.data.userType, data);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 查询某个学生的所有危机档案接口
 */
router.post('/getHarmRecordByStudent', async function (req, res, next) {
    let studentId = req.body.studentId;
    if (!studentId) {
        res.status(400).json({ success: false, msg: "参数不足，查询失败" });
        return ;
    }
    let result = await harmService.getHarmRecordByStudent(req.jwtResult.data.uid, req.jwtResult.data.userType, studentId);
    res.status(result.success ? 200 : 400).json(result);
});

module.exports = router;
